package base.iso
{
	import flash.geom.Point;

	public class IsoBox extends IsoDisplayObject
	{
		private var _height:Number;
		private var _color:uint;
		private var _point:Point;
		private var _isoPoint:IsoPoint;
		
		public function IsoBox(size:Number, height:Number, color:uint=0xFCFCFC)
		{
			_height=height;
			_color=color;
			_size=size;
			draw();
		}
		
		public function update():void
		{
			draw();
			resetPosition();
		}
		
		private function draw():void
		{
			graphics.clear();
			var red:int = _color >> 16;
			var green:int = _color >> 8 & 0xff;
			var blue:int = _color & 0xff;
			
			var leftShadow:uint = (red * .5) << 16 |
				(green * .5) << 8 |
				(blue * .5);
			var rightShadow:uint = (red * .75) << 16 |
				(green * .75) << 8 |
				(blue * .75);
			
			var h:Number = _height * Y_CORRECT;
			//顶
			graphics.beginFill(_color);
			graphics.lineStyle(0, 0, .5);
			graphics.moveTo(-_size, -h+_size/2);
			graphics.lineTo(0, -_size * .5 - h+_size/2);
			graphics.lineTo(_size, -h+_size/2);
			graphics.lineTo(0, _size * .5  - h+_size/2);
			graphics.lineTo(-_size, -h+_size/2);
			graphics.endFill();
			
			//左
			graphics.beginFill(leftShadow);
			graphics.lineStyle(0, 0, .5);
			graphics.moveTo(-_size, -h+_size/2);
			graphics.lineTo(0, _size * .5 - h+_size/2);
			graphics.lineTo(0, _size * .5+_size/2);
			graphics.lineTo(-_size, 0+_size/2);
			graphics.lineTo(-_size, -h+_size/2);
			graphics.endFill();
			
			//右
			graphics.beginFill(rightShadow);
			graphics.lineStyle(0, 0, .5);
			graphics.moveTo(_size, -h+_size/2);
			graphics.lineTo(0, _size * .5 - h+_size/2);
			graphics.lineTo(0, _size * .5+_size/2);
			graphics.lineTo(_size, 0+_size/2);
			graphics.lineTo(_size, -h+_size/2);
			graphics.endFill();
		}

		
		private function resetPosition():void
		{
			x=Math.floor(_isoPoint.x/_size)*_size;
			y=_isoPoint.y;
			z=Math.floor(_isoPoint.z/_size)*_size;
		}
		
		public function get point():Point
		{
			return _point;
		}
		
		public function set point(value:Point):void
		{
			_point = value;
			_isoPoint=IsoUtils.screenToSpace(_point);
			resetPosition();
		}
		
		public function get isoPoint():IsoPoint
		{
			return _isoPoint;
		}
		
		public function set isoPoint(value:IsoPoint):void
		{
			_isoPoint = value;
			_point=IsoUtils.spaceToScreen(_isoPoint);
			resetPosition();
		}
		
		override public function dispose():void
		{
			graphics.clear();
			super.dispose();
		}

		public function get color():uint
		{
			return _color;
		}

		public function set color(value:uint):void
		{
			_color = value;
		}

		override public function get height():Number
		{
			return _height;
		}

		override public function set height(value:Number):void
		{
			_height = value;
		}
	}
}